package com.sgu6.sgu6_machine.ui.fragment.set;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sgu6.middleware.enums.MachineCmdEnum;
import com.sgu6.middleware.util.HexUtil;
import com.sgu6.serialport.manager.SerialPortManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.ui.views.AmountView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 警报设置
 */
public class MillSetF extends BaseF {

    @BindView(R.id.atDropDelay)
    AmountView atDropDelay;
    @BindView(R.id.atPullDelay)
    AmountView atPullDelay;
    @BindView(R.id.atSealDelay)
    AmountView atSealDelay;

    Map<String, String> defaultValue = new HashMap<>();
    private Thread thread;

    public static MillSetF newInstance() {
        return new MillSetF();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mill_set, container, false);
        unbinder = ButterKnife.bind(this, v);

        initUI();
        return v;
    }

    public void initUI() {
        atDropDelay.setDefaultValue(MachineContainer.machineConfigDto.dropDelay);
        atPullDelay.setDefaultValue(MachineContainer.machineConfigDto.pullDelay);
        atSealDelay.setDefaultValue(MachineContainer.machineConfigDto.sealDelay);

        atDropDelay.setAmountMaxValue(10);
        atPullDelay.setAmountMaxValue(10);
        atSealDelay.setAmountMaxValue(10);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btnSetDropDelay, R.id.btnSetPullDelay, R.id.btnSetSealDelay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnSetDropDelay:
                String hc = String.format(MachineCmdEnum.下米延迟.code(), atDropDelay.getValue() * 10);
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(hc));
                MachineContainer.machineConfigDto.dropDelay = (atDropDelay.getValue());
                updateConfig();
                break;
            case R.id.btnSetPullDelay:
                String vc = String.format(MachineCmdEnum.拉膜延迟.code(), atPullDelay.getValue() * 10);
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(vc));
                MachineContainer.machineConfigDto.pullDelay = (atPullDelay.getValue());
                updateConfig();
                break;
            case R.id.btnSetSealDelay:
                String oc = String.format(MachineCmdEnum.封膜延迟.code(), atSealDelay.getValue() * 10);
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(oc));
                MachineContainer.machineConfigDto.sealDelay = (atSealDelay.getValue());
                updateConfig();
                break;
        }
    }

}
